package com.certificationManager.client.presenter;

import com.certificationManager.client.CMEventBus;
import com.certificationManager.client.i18n.AppConstants;
import com.certificationManager.client.presenter.view_interface.ILoginView;
import com.certificationManager.client.service.UserServiceAsync;
import com.certificationManager.client.util.CookieUtils;
import com.certificationManager.client.util.Locale;
import com.certificationManager.client.util.LocaleUtils;
import com.certificationManager.client.view.LoginView;
import com.certificationManager.client.view.TopView;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mvp4g.client.annotation.InjectService;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;


/**
 * @author Benjamin Barbier
 *
 */
@Presenter( view = LoginView.class )
public class LoginPresenter extends BasePresenter<ILoginView, CMEventBus> {

	/** Internationalization */
	AppConstants i18n = GWT.create(AppConstants.class);
	
	/** Services */
	private UserServiceAsync userService = null;
	
	@Override
	public void bind(){
		
		view.getLoginButton().addListener(Events.OnClick, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				userService.isRegistered(
				view.getEmailField().getValue(),
				view.getPasswordField().getValue(), 
				new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							String userId = view.getEmailField().getValue();
							Cookies.setCookie(CookieUtils.USERID_COOKIE, userId, CookieUtils.getInstance().getDuration());
							eventBus.goToMainStructure();
						} else {
							MessageBox.alert("Erreur", i18n.login_connectKO(), null);
						}
						init();
					}
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Erreur", i18n.login_PbTech(), null);
						init();
					}
				}
			);}
		});
		
		view.getStartButton().addListener(Events.OnClick, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				eventBus.goToRegistration();
			}
		});
		
		view.getFlagFR().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
		        changeLanguage(Locale.FRENCH);
			}
		});
		
		view.getFlagEN().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
		        changeLanguage(Locale.ENGLISH);
			}
		});
		
		view.getFlagIT().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
		        changeLanguage(Locale.ITALIAN);
			}
		});
		
		view.getFlagSP().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
		        changeLanguage(Locale.SPANISH);
			}
		});
		
		view.getFlagDE().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				changeLanguage(Locale.GERMAN);
			}
		});
		
		view.getImageInfo().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				MessageBox.info("Info", "Language: "+LocaleUtils.getInstance().getLocale().getLanguage(), null);		
			}
		});
		
	}
	
	private void init() {
		view.getPasswordField().setValue("");
	}
	
	public void onStart(){
		view.getEmailField().setValue("");
		view.getPasswordField().setValue("");
		eventBus.changeBody(view.getViewWidget());
	}
	
	public void onGoToLogin(){
		eventBus.changeBody(view.getViewWidget());
	}
	
	public void changeLanguage(Locale locale) {
		Cookies.setCookie(CookieUtils.LANGUAGE_COOKIE, locale.getLanguage(), CookieUtils.getInstance().getDuration());
		UrlBuilder builder = Location.createUrlBuilder().setParameter("locale", locale.getLanguage());
        Window.Location.replace(builder.buildString());
	}
	
	@InjectService
	public void setUserService( UserServiceAsync userService ) {
		this.userService = userService;
	}
	
}
